﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Windows.Forms;
using BigTestV3.common;
using BigTestV3.xmlHelp;
using RemotingSocket;

namespace BigTestV3
{
    public partial class frmTec : Form
    {       
        public frmTec()
        {
            InitializeComponent();
        }
        
        /// <summary>
        /// 基本信息事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void BasicSetClick(object sender, EventArgs e)
        {
            if (this.MdiChildren.Count()>=1)
            {
                foreach (Form f in this.MdiChildren)
                {
                  f.Close();  
                }
                frmBasicSet basicSet = new frmBasicSet();
                basicSet.MdiParent = this;
                basicSet.Show();
            }
            else
            {
                frmBasicSet basicSet = new frmBasicSet();
                basicSet.MdiParent = this;
                basicSet.Show();
            }
        }
       
        /// <summary>
        /// 传输文件事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void TransferClick(object sender, EventArgs e)
        {
            string strPath = FilePathTotal.PathPath;
            FilePath filePath = null;
            if (File.Exists(strPath))
            {
                filePath= XmlHelper.XmlDeserializeFromFile<FilePath>(strPath, Encoding.UTF8);
            }

            if (filePath != null && File.Exists(filePath.Path))
            {
                BasicSetting setting = XmlHelper.XmlDeserializeFromFile<BasicSetting>(filePath.Path, Encoding.UTF8);
            }

            _netFileTransferServer.Start();

            MessageBox.Show("已启动传输");

        }

        /// <summary>
        /// 考场导入事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void TestImportClick(object sender, EventArgs e)
        {

            if (File.Exists(FilePathTotal.PathPath))
            {
                FilePath filePath = XmlHelper.XmlDeserializeFromFile<FilePath>(FilePathTotal.PathPath, Encoding.UTF8);
                if (File.Exists(filePath.Path))
                {
                    BasicSetting setting = XmlHelper.XmlDeserializeFromFile<BasicSetting>(filePath.Path, Encoding.UTF8);


                    dlg = new OpenFileDialog();
                    if (dlg.ShowDialog() == DialogResult.OK)
                    {
                        if (dlg.FileName.Contains(".xml"))
                        {
                            string tarfile = setting.FilePath + @"\TestInfo.xml";
                            FileExist(dlg.FileName, tarfile);
                            FileExist(dlg.FileName, FilePathTotal.BigTestInfo);
                            if (File.Exists(tarfile))
                            {
                                MessageBox.Show("加载成功");
                            }
                        }

                    }
                }
                else
                {
                    MessageBox.Show("请设置基本信息");
                }
            }
            else
            {
                MessageBox.Show("请设置基本信息");
            }
        }
       
        /// <summary>
        /// 题目导入设置
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void PaperImportClick(object sender, EventArgs e)
        {
            if (File.Exists(FilePathTotal.PathPath))
            {
                FilePath filePath = XmlHelper.XmlDeserializeFromFile<FilePath>(FilePathTotal.PathPath, Encoding.UTF8);
                if (File.Exists(filePath.Path))
                {
                    BasicSetting setting = XmlHelper.XmlDeserializeFromFile<BasicSetting>(filePath.Path, Encoding.UTF8);

                    dlg = new OpenFileDialog();
                    if (dlg.ShowDialog() == DialogResult.OK)
                    {
                        if (dlg.FileName.Contains(".html") || dlg.FileName.Contains(".htm") ||
                            dlg.FileName.Contains(".mht"))
                        {
                            string tarfile = setting.FilePath + @"\StuTest.htm";
                            FileExist(dlg.FileName, tarfile);
                           
                            if (File.Exists(tarfile))
                            {
                                MessageBox.Show("加载成功");
                            }
                        }
                    }
                }
                else
                {
                    MessageBox.Show("请设置基本信息");
                }
            }
            else
            {
                MessageBox.Show("请设置基本信息");
            }
        }       

        /// <summary>
        /// 学生状态查询事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void StuStatusClick(object sender, EventArgs e)
        {           
            if (this.MdiChildren.Count()>=1)
            {
                foreach (Form f in this.MdiChildren)
                {
                  f.Close();  
                }
                frmStatus status = new frmStatus();
                status.MdiParent = this;
                status.Show();
            }
            else
            {
                frmStatus status = new frmStatus();
                status.MdiParent = this;
                status.Show();
            }
        }

        /// <summary>
        /// 判断文件是否存在
        /// </summary>
        /// <param name="sorfile"></param>
        /// <param name="tarfile"></param>
        private void FileExist(string sorfile, string tarfile)
        {
            if (File.Exists(tarfile))
            {
                File.Delete(tarfile);

            }
            File.Copy(sorfile, tarfile);
        }

        /// <summary>
        /// 关闭传输事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void TransfireStopClick(object sender, EventArgs e)
        {
           _netFileTransferServer.Stop();

            MessageBox.Show("关闭成功");
        }

        private void FileDownLoadClick(object sender, EventArgs e)
        {
            if (this.MdiChildren.Count() >= 1)
            {
                foreach (Form f in this.MdiChildren)
                {
                    f.Close();
                }
                frmDownload download = new frmDownload();
                download.MdiParent = this;
                download.Show();
            }
            else
            {
                frmDownload download = new frmDownload();
                download.MdiParent = this;
                download.Show();
            }
        }

        private void UploadClick(object sender, EventArgs e)
        {
            if (this.MdiChildren.Count() >= 1)
            {
                foreach (Form f in this.MdiChildren)
                {
                    f.Close();
                }
                frmUpload upload = new frmUpload();
                upload.MdiParent = this;
                upload.Show();
            }
            else
            {
                frmUpload upload = new frmUpload();
                upload.MdiParent = this;
                upload.Show();
            }
        }

       
    }
}
